﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK19.DB;
using PTTK19.BUS;
using DAO_ADMIN;
namespace PTTK19.GUI
{
    public partial class MonAnFrm : Form
    {
        public int _MaMonAn;
        public MonAnFrm()
        {
            InitializeComponent();
            loadDgvMonAn();
        }
        //load danh sach mon an
        public void loadDgvMonAn()
        {
            DataTable dt = DBMonAn.getListMonAn();
            dgvMonAn.DataSource = dt;
        }
        private void dgvMonAn_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }
        public void LoadThongTinMonAn(int a, string b, string c)
        {
            txtMaMonAn.Text = a.ToString();
            txtTenMonAn.Text = b.ToString();
            txtCachCheBien.Text = c.ToString();
        }
        private void dgvNguyenLieu_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int index = dgvMonAn.CurrentRow.Index;
                if (dgvMonAn.CurrentRow != null)
                {
                    //lay ma mon an
                    _MaMonAn = (int)dgvMonAn.Rows[index].Cells[0].Value;
                }
            }
            catch (Exception ex)
            { }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvNguyenLieu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string command = dgvNguyenLieu.Columns[e.ColumnIndex].Name;
                if (command == "TuyChon") // colbtn là tên cột chứa button
                {
                    ChiTietNguyenLieuFrm ctFrm = new ChiTietNguyenLieuFrm();
                    //ctFrm.MaNguyenLieu = _MaNguyenLieu;
                    //ctFrm.txtMaMonAn.Text = _MaMonAn.ToString();
                    ctFrm.ShowDialog();
                }
            }
        }

        private void btnCapNhatMonAn_Click(object sender, EventArgs e)
        {
            CapNhatMonAnFrm frm = new CapNhatMonAnFrm();
            frm.txtMaMonAn.Text = txtMaMonAn.Text;
            frm.txtTenMonAn.Text = txtTenMonAn.Text;
            frm.txtCachCheBien.Text = txtCachCheBien.Text;
            frm.ShowDialog();
        }

        private void btmRefreh_Click(object sender, EventArgs e)
        {
            loadDgvMonAn();
        }

        private void btnThemMonAn_Click(object sender, EventArgs e)
        {
            ThemMonAnFrm frm = new ThemMonAnFrm();
            frm.txtTenMonAn.Text = txtTenMonAn.Text;
            frm.txtCachCheBien.Text = txtCachCheBien.Text;
            frm.ShowDialog();
        }

        private void btnXoaMonAn_Click(object sender, EventArgs e)
        {
            int _MaMonAn = int.Parse(txtMaMonAn.Text);
            DBMonAn.HuyMonAn(_MaMonAn);
            MessageBox.Show("Hủy món ăn thành công", "Thông báo");
        }

        private void dgvMonAn_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataGridViewRow currentrow = dgvMonAn.CurrentRow;
                if (currentrow == null)
                {
                    return;
                }
                int index = dgvMonAn.CurrentRow.Index;
                if (dgvMonAn.CurrentRow != null)
                {
                    //lay ma mon an
                    _MaMonAn = (int)dgvMonAn.Rows[index].Cells[0].Value;
                    string _TenMonAn = dgvMonAn.Rows[index].Cells[1].Value.ToString();
                    string _CachCheBien = dgvMonAn.Rows[index].Cells[2].Value.ToString();
                    //load danh sach nguyen lieu cua mon an 
                    DataTable dt = DBNguyenLieu.getListNguyenLieuTheoMonAn(_MaMonAn);
                    dgvNguyenLieu.DataSource = dt;
                    //Load thong tin mon an len texbox
                    LoadThongTinMonAn(_MaMonAn, _TenMonAn, _CachCheBien);
                }
            }
            catch (Exception ex)
            { }
        }
    }
}
